package com.oa.ylms.apps.actions.news;

import java.util.List;

import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.news.INewsCategoryService;
import com.oa.ylms.apps.business.news.INewsInfoService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.news.NewsCategoryValue;
import com.oa.ylms.apps.entity.news.NewsInfoValue;

@SuppressWarnings("serial")
public class NewsInfoAction extends BasicAction {

	private List<NewsInfoValue> infoList;
	private List<NewsCategoryValue> categoryList;
	private NewsInfoValue newsInfo;
	private INewsInfoService infoService;
	private INewsCategoryService categoryService;
	
	private Integer newsId;
	private String newsIds;
	private Integer categoryId;
	private String publishTime;
	private String staffName;
	private String title;
	private String content;
	
	private String str;
 
	// Action method

	public String execute() throws Exception {
		newsInfo = infoService.queryNewsById(newsId);
		return "show";
	}
	
	public String show() throws Exception {
		newsInfo = infoService.queryNewsById(newsId);
		return "showNews";
	}
	
	
	public String toAdd() throws Exception {
		categoryList = categoryService.queryCategoryByStates(IDEDStates.ENABLE);
		return "toAdd";
	}
	
	public String add() throws Exception{
		staffInfo = (StaffInfoValue)session.get("login_staffInfo");
		content = content.replace("<", "&lt;").replace(">", "&gt;").replace("\"", "'");
		if(infoService.addNewsInfo(categoryId, title, content, staffInfo) > 0){
			str = "SUCCESS!";
			return "add";
		}	
		return "false";
	}
	
	public String toEdit() throws Exception {
		categoryList = categoryService.queryCategoryByStates(IDEDStates.ENABLE);
		newsInfo = infoService.queryNewsById(newsId);
		title = newsInfo.getTitle();
		content = newsInfo.getContent();
		categoryId = newsInfo.getNewsCategory().getCategoryId();
		return "toEdit";
	}
	
	public String modify() throws Exception{
		staffInfo = (StaffInfoValue)session.get("login_staffInfo");
		content = content.replace("<", "&lt;").replace(">", "&gt;").replace("\"", "'");
		if(infoService.modifyNewInfo(staffInfo, newsId, categoryId, title, content))
			return "modify";
		return "false";
	}
	
	public String toManager() throws Exception{
		if("".equals(publishTime))
			publishTime = null;
		if("".equals(title))
			title = null;
		if("".equals(staffName))
			staffName = null;
		
//		if(publishTime != null)
//			publishTime = new String(publishTime.getBytes("ISO-8859-1"),"UTF-8");
//		if(title != null)
//			title = new String(title.getBytes("ISO-8859-1"),"UTF-8");
//		if(staffName != null)
//			staffName = new String(staffName.getBytes("ISO-8859-1"),"UTF-8");
		
		totalpage = infoService.countTPages(categoryId, publishTime, title, staffName, rows);
		count = infoService.getTotalNumber();
		if(count%rows==0&&totalpage+1==currpage){
				currpage=totalpage;
		}
		categoryList = categoryService.queryCategoryByStates(IDEDStates.ENABLE);
		infoList = infoService.queryNewsWithPage(categoryId, publishTime, title, staffName, rows, currpage);
		return "toManager";
	}
	
	public String delete() throws Exception{
		if(infoService.deleteNewsInfo(newsIds)){
			return "delete";
		}
		return "false";

	}
	
	public String toShow() throws Exception{
		if("".equals(publishTime))
			publishTime = null;
		if("".equals(title))
			title = null;
		if("".equals(staffName))
			staffName = null;
		
//		if(publishTime != null)
//			publishTime = new String(publishTime.getBytes("ISO-8859-1"),"UTF-8");
//		if(title != null)
//			title = new String(title.getBytes("ISO-8859-1"),"UTF-8");
//		if(staffName != null)
//			staffName = new String(staffName.getBytes("ISO-8859-1"),"UTF-8");
		
		totalpage = infoService.countTPages(categoryId, publishTime, title, staffName, rows);
		count = infoService.getTotalNumber();
		categoryList = categoryService.queryCategoryByStates(IDEDStates.ENABLE);
		infoList = infoService.queryNewsWithPage(categoryId, publishTime, title, staffName, rows, currpage);
		return "toShow";
	}
	
	public String toShowNews() throws Exception{
		if("".equals(publishTime))
			publishTime = null;
		if("".equals(title))
			title = null;
		if("".equals(staffName))
			staffName = null;
		
//		if(publishTime != null)
//			publishTime = new String(publishTime.getBytes("ISO-8859-1"),"UTF-8");
//		if(title != null)
//			title = new String(title.getBytes("ISO-8859-1"),"UTF-8");
//		if(staffName != null)
//			staffName = new String(staffName.getBytes("ISO-8859-1"),"UTF-8");
		
		totalpage = infoService.countTPages(categoryId, publishTime, title, staffName, rows);
		count = infoService.getTotalNumber();
		categoryList = categoryService.queryCategoryByStates(IDEDStates.ENABLE);
		infoList = infoService.queryNewsWithPage(categoryId, publishTime, title, staffName, rows, currpage);
		return "toShowNews";
	}
	
	// Action property accessors
	
	public Integer getNewsId() {
		return newsId;
	}

	public void setNewsId(Integer newsId) {
		this.newsId = newsId;
	}
	
	public String getPublishTime() {
		return publishTime;
	}

	public void setPublishTime(String publishTime) {
		this.publishTime = publishTime;
	}

	public List<NewsInfoValue> getInfoList() {
		return infoList;
	}

	public void setInfoList(List<NewsInfoValue> infoList) {
		this.infoList = infoList;
	}

	public List<NewsCategoryValue> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<NewsCategoryValue> categoryList) {
		this.categoryList = categoryList;
	}

	public INewsInfoService getInfoService() {
		return infoService;
	}

	public void setInfoService(INewsInfoService infoService) {
		this.infoService = infoService;
	}
	
	public INewsCategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(INewsCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}


	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}


	public String getStaffName() {
		return staffName;
	}


	public void setNewsIds(String newsIds) {
		this.newsIds = newsIds;
	}


	public String getNewsIds() {
		return newsIds;
	}


	public void setNewsInfo(NewsInfoValue newsInfo) {
		this.newsInfo = newsInfo;
	}


	public NewsInfoValue getNewsInfo() {
		return newsInfo;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public String getStr() {
		return str;
	} 
}
